package m.fun.service;

import m.base.model.UserInfo;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryPage;
import m.common.service.Service;
import m.fun.model.GoodsOffer;
import m.store.model.StoreGoods;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.NumberUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsOfferService extends Service {
    public void addStoreGoods(UserInfo user, List<StoreGoods> goodsList) throws MException, SQLException {
        if(goodsList.isEmpty()) throw new MException(this.getClass(),"请选择要添加的商品");
        List<GoodsOffer> ls=new ArrayList<>();
        for(StoreGoods goods : goodsList){
            if(ModelQueryList.getModelList(GoodsOffer.class,new String[]{"oid"},new QueryPage(0,1),
                QueryCondition.and(
                    QueryCondition.eq("user.oid",user.getOid()),
                    QueryCondition.eq("goods.oid",goods.getOid())
                )
            ).isEmpty()){
                GoodsOffer model=new GoodsOffer();
                model.setOid(GenerateID.generatePrimaryKey());
                model.setUser(user);
                model.setGoods(goods);
                model.setIndexTxt("N");
                ls.add(model);
            }
        }
        if(!ls.isEmpty()) ModelUpdateUtil.insertModels(ls.toArray(new GoodsOffer[0]));
    }
}
